package com.itheima.web;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.ReportVo;
import com.itheima.entity.Result;
import com.itheima.service.ChartReportService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Author: LIHAO
 * @Description:
 * @Date: Create in 15:17 2020/2/10
 */
@RequestMapping("/report")
@RestController
//写在这里springmvc  能扫描到吗？
//写的时候想了没

public class ChartReport {
    @Reference
    private ChartReportService chartReportInterface;

    /**
     * 统计分析 会员数量
     * @return
     */
    @RequestMapping("/getMemberReport")
    public Result getMemberReport( @RequestBody(required = false) ReportVo reportVo){
        //获取当前时间
        Date date = new Date();
        //        默认12个月
        long lMonth=12;
        DateTime dateTime=DateUtil.offsetMonth(date, -12);;
//        获取起止时间
        if (reportVo!=null){
            Date start = reportVo.getStart();
            Date end = reportVo.getEnd();
            if (start!=null&&end!=null){
                lMonth=  DateUtil.betweenMonth(start, end,false);
                 dateTime = DateTime.of(start);
            }
        }
//        1.获取过去一年的月份，并放入lisit集合中
        ArrayList<String> months = new ArrayList<>();
        for (int i = 0; i <= lMonth ; i++) {
            DateTime dateTime1 = DateUtil.offsetMonth(dateTime, i);
            String month = dateTime1.toString("yyyy-MM");
            months.add(month);
        }
//        2.根据查询的月份获取会员的数量
        List<Integer> memberCount=chartReportInterface.getMemberReport(months);
//       3. 返回一个map对象导入到结果map集里
        Map map = new HashMap<>();
        map.put("months",months);
        map.put("memberCount",memberCount);
        return new Result(true,"查询成功",map);
    }

    /**
     * 统计分析套餐占比
     * @return
     */
    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){

        List<Map> maps= chartReportInterface.chartReportInterface();

        return new Result(true,"查询成功",maps);

    }

    /**
     * 会员年龄及性别分析
     */
    @RequestMapping("/getMemberSexAndAge")
    public Result getMemberSexAndAge(){
    //  获取会员性别
        List<Map> mapSex = chartReportInterface.getSex();
   //获取会员年龄段
        List<Map>  mapAge=chartReportInterface.getAge();

      Map map=  new HashMap<>();
      map.put("mapSex",mapSex);
      map.put("mapAge",mapAge);
        return new Result(true,"查询成功",map);
    }
}
